﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace SnippetsSidebarPlugin
{
    public partial class Snippets : UserControl
    {
        Notepad_X.Main main = Notepad_X.Static.MainInstance;
        String SnippetsPath = Notepad_X.Main.System_ROOT + "\\Plugins\\Snippets.xml";

        public Snippets()
        {
            InitializeComponent();
        }

        public void addNewItem(String Data)
        {

            ListViewItem i = new ListViewItem();
            String Label = Data.Replace("\n", "").Replace("\r", "");
            if (Data.Length > 25)
            {
                Label = Label.Substring(0, 22) + "...";
            }
            i.Text = Label;
            i.SubItems.Add(Data);
            i.ToolTipText = Data;

            SnippetList.Items.Add(i);
        }
        public void AddSelectedItemToForm()
        {
            int id = main.get_selectedID();
            if (id != -1)
            {
                main.Documents[id].insert(main.Documents[id].selectionIndex(),SnippetList.SelectedItems[0].SubItems[1].Text);
            }
        }

        private void SnippetList_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void SnippetList_DragDrop(object sender, DragEventArgs e)
        {
            //if(e.Data.GetType() == typeof(String)      )
            addNewItem((String)e.Data.GetData(typeof(string)));
            e.Effect = DragDropEffects.None;
        }

        //private void SnippetList_DragEnter(object sender, DragEventArgs e)
        //{
        //    if (SnippetList.SelectedItems.Count > 0)
        //    {
        //        e.Effect = DragDropEffects.Copy;
        //        DoDragDrop(SnippetList.SelectedItems[0].SubItems[1].Text, DragDropEffects.Copy);
        //        //e.Data.SetData(typeof(String), SnippetList.SelectedItems[0].SubItems[1].Text);
        //    }
        //}
        public void SaveSnippets()
        {
            XmlTextWriter xmlFile = new XmlTextWriter(SnippetsPath, System.Text.Encoding.UTF8);

            xmlFile.Formatting = Formatting.Indented;

            xmlFile.WriteStartElement("NotepadX");

            xmlFile.WriteStartElement("Snippets");

            foreach (ListViewItem Item in SnippetList.Items)
            {

                xmlFile.WriteStartElement("Snippet");
                xmlFile.WriteCData(Item.SubItems[1].Text);
                xmlFile.WriteEndElement();
            }
            xmlFile.WriteEndElement();

            xmlFile.WriteEndElement();

            xmlFile.Close();
        }

        private void Snippets_Load(object sender, EventArgs e)
        {
            SnippetList.Items.Clear();

            if (System.IO.File.Exists(SnippetsPath))
            {
                XmlDocument xmlFile = new XmlDocument();
                xmlFile.Load(SnippetsPath);

                XmlNodeList nodes;

                nodes = xmlFile.GetElementsByTagName("Snippets");
                if (nodes.Count == 1)
                {
                    foreach (XmlElement node in nodes[0].ChildNodes)
                    {
                        addNewItem(node.InnerText);
                    }
                }
            }
            menu_select.Text = Notepad_X.Main.app_language.GetString(Notepad_X.Classes.Strings.Select);
            menu_remove.Text = Notepad_X.Main.app_language.GetString(Notepad_X.Classes.Strings.Remove);
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (SnippetList.SelectedItems.Count > 0)
            {
                SnippetList.Items.Remove(SnippetList.SelectedItems[0]);
            }
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddSelectedItemToForm();
        }



        private void SnippetList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (SnippetList.SelectedItems.Count > 0)
            {
               AddSelectedItemToForm();
            }
        }

      
    }
}
